<template>
  <div class="overview-style">
    <div class="top-select-group">
      <el-input placeholder="客户标签名称" v-model="searchKeyword" clearable style="width: 216px"></el-input>
      <el-button type="primary">搜索</el-button>
      <el-button style="float:right;" @click="handlePartialDelete">批量删除</el-button>
      <el-button style="float:right;" type="primary" @click="addDialogVisible=true">添加标签</el-button>
    </div>

    <div class="middle-table-group">
      <el-table
        :header-cell-style="{'background-color':'#e6f1ff','height':'62px'}"
        ref="multipleTable"
        :data="tableData"
        v-loading="isLoading"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" align="center"></el-table-column>
        <el-table-column prop="tagName" label="标签预览" align="center">
          <template slot-scope="scope">
            <el-tag effect="dark" :color="scope.row.color">{{scope.row.tagName}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="useCount" label="使用次数" align="center"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="getEditForm(scope.row)" type="text" size="normal">编辑</el-button>
            <el-button @click="getDeleteForm(scope.row)" type="text" size="normal">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="bottom-pagination-group">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, prev, pager, next,sizes, jumper"
          :total="totalData"
        ></el-pagination>
      </div>
    </div>

    <el-dialog title="添加标签" top="9vh" :visible.sync="addDialogVisible" width="33%">
      <div class="add-dialog-style">
        <el-form ref="addOrderForm" :model="addOrderForm" label-position="left" label-width="120px">
          <el-form-item :rules="{ message: '请输入标签名称'}" label="标签名称">
            <el-input style="width:100%;" v-model="addOrderForm.tagName" placeholder="请输入标签名称"></el-input>
          </el-form-item>
          <el-form-item label="选择标签颜色">
            <el-radio-group v-model="addOrderForm.color">
              <el-radio
                style="padding:10px"
                v-for="tagColor in tagColors"
                :key="tagColor"
                :label="tagColor"
              >
                <el-tag :color="tagColor" effect="dark">&nbsp;&nbsp;&nbsp;&nbsp;</el-tag>
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native.prevent="handleClean" type="text" size="normal">清空</el-button>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateTag">保存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑标签" top="9vh" :visible.sync="editDialogVisible" width="33%">
      <div class="add-dialog-style">
        <el-form
          ref="editOrderForm"
          :model="editOrderForm"
          label-position="left"
          label-width="120px"
        >
          <el-form-item :rules="{ message: '请输入标签名称'}" label="标签名称">
            <el-input style="width:100%;" v-model="editOrderForm.tagName" placeholder="请输入标签名称"></el-input>
          </el-form-item>
          <el-form-item label="选择标签颜色">
            <el-radio-group v-model="editOrderForm.color">
              <el-radio
                style="padding:10px"
                v-for="tagColor in tagColors"
                :key="tagColor"
                :label="tagColor"
              >
                <el-tag :color="tagColor" effect="dark">&nbsp;&nbsp;&nbsp;&nbsp;</el-tag>
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native.prevent="handleClean" type="text" size="normal">清空</el-button>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleUpdateTag">保存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="删除确认" :visible.sync="deleteDialogVisible" top="34vh" width="26%">
      <div class="delete-dialog-style">删除后无法恢复，确定删除此信息吗？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleDeleteTag">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from "@/api/api";
export default {
  name: "SettingCustomerTag",
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      addDialogVisible: false,
      editDialogVisible: false,
      deleteDialogVisible: false,
      addOrderForm: {},
      editOrderForm: {},
      currentPage: 1,
      pageSize: 10,
      totalData: 0,
      deleteId: 0,
      isLoading: false,
      multipleSelection: [],
      tagColors: [
        "#006eff",
        "#f46363",
        "#2ed477",
        "#fe9b4b",
        "#d076d0",
        "#1aa3fe",
        "#9d7115",
        "#7676fe",
        "#1a7676"
      ]
    };
  },
  watch: {},
  computed: {},
  methods: {
    handleSizeChange(size) {
      this.pageSize = size;
      this.getPageData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getPageData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getEditForm(data) {
      this.editDialogVisible = true;
      this.editOrderForm = data;
    },
    getPageData() {
      this.isLoading = true;
      api
        .getAllTagAPI({
          page: this.currentPage,
          limit: this.pageSize
        })
        .then(res => {
          this.isLoading = false;
          let { error, data } = res.data;
          if (error) {
            this.$message.error("请求接口异常");
          } else {
            this.totalData = data.total;
            this.tableData = data.page;
          }
        });
    },
    handleCreateTag() {
      this.$refs.addOrderForm.validate(valid => {
        if (valid) {
          this.addDialogVisible = false;
          api
            .createTagAPI({
              tagName: this.addOrderForm.tagName,
              tagType: "类型",
              color: this.addOrderForm.color,
              useCount: 0
            })
            .then(res => {
              let { error, data } = res.data;
              if (error) {
                this.$message.error(data);
              } else {
                this.getPageData();
                this.$message({
                  message: "标签创建成功",
                  type: "success"
                });
              }
            });
        } else {
          this.$message.error("表单填写有误");
        }
      });
    },
    handleUpdateTag() {
      this.$refs.editOrderForm.validate(valid => {
        if (valid) {
          this.editDialogVisible = false;
          api.updateTagAPI(this.editOrderForm).then(res => {
            let { error, data } = res.data;
            if (error) {
              this.$message.error(data);
            } else {
              this.getPageData();
              this.$message({
                message: "更新标签成功",
                type: "success"
              });
            }
          });
        } else {
          this.$message.error("表单填写有误");
        }
      });
    },
    getDeleteForm(data) {
      console.log(data);

      this.deleteDialogVisible = true;
      this.deleteId = data.id;
    },
    handleDeleteTag() {
      this.deleteDialogVisible = false;
      api.deleteTagAPI(this.deleteId).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.getPageData();
          this.$message({
            message: "删除标签成功",
            type: "success"
          });
        }
      });
    },
    handlePartialDelete() {
      let params = this.multipleSelection.map(item => {
        return item.id;
      });
      params = params.join(",");
      api.partialDeleteTagAPI(params).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.multipleSelection = [];
          this.getPageData();
          this.$message({
            message: "标签信息删除成功",
            type: "success"
          });
        }
      });
    },
  },
  created() {
    this.getPageData();
  },
  mounted() {}
};
</script>

<style scoped>
.overview-style {
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
}
.bottom-pagination-group {
  padding: 20px 20px 0;
  float: right;
}
.middle-table-group {
  margin-top: 20px;
  display: inline-block;
  width: 100%;
}
.add-dialog-style {
  height: 250px;
  overflow: auto;
  padding: 20px 10px;
  margin-top: 10px;
  border-top: 1px solid #f2f2f2;
  border-bottom: 1px solid #f2f2f2;
}
.delete-dialog-style {
  border-top: 1px solid #f2f2f2;
  border-bottom: 1px solid #f2f2f2;
  padding: 20px;
  padding-bottom: 70px;
}
</style>
<style>
.el-tag--dark {
  border: none;
}
</style>
